import request from './request'
import type { ApiResponse } from './types'

// 登录请求参数
export interface LoginParams {
  username: string
  password: string
}

// 登录响应数据
export interface LoginResponse {
  token: string
  userInfo: {
    id: number
    username: string
    realName: string
    avatar: string
    role: string
    permissions: string[]
    departmentId: number
    position: string
  }
}

// 用户登录
export const login = (params: LoginParams) => {
  return request<ApiResponse<LoginResponse>>({
    url: '/user/login',
    method: 'post',
    data: params
  })
}

// 获取用户信息
export const getUserInfo = () => {
  return request<ApiResponse<LoginResponse['userInfo']>>({
    url: '/user/info',
    method: 'get'
  })
}

// 退出登录
export const logout = () => {
  return request<ApiResponse<boolean>>({
    url: '/user/logout',
    method: 'post'
  })
}

// 获取用户列表
export const getUserPage = (params: {
  current: number
  size: number
}) => {
  return request({
    url: '/user/page',
    method: 'post',
    data: params
  })
}

// user update
export const updateUser = (params: {
  id: number
  username: string
  realName: string
  phone: string
  password?: string
}) => {
  return request({
    url: '/user/update',
    method: 'post',
    data: params
  })
}

// user add
export const addUser = (params: {
  username: string
  realName: string
  phone: string
  password?: string
}) => {
  return request({
    url: '/user/add',
    method: 'post',
    data: params
  })
}
